<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <el-row type="flex" justify="space-between" align="middle" style="height: 40px">
          <el-col :span="20">
            <span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span>
                    <svg-icon icon-class="edit" style="margin-right:5px;" />
                    操作<i class="el-icon-arrow-down" />
                  </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item @click.native="addSonDepartment('')">添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>

        <el-tree
          default-expand-all
          :data="list"
        >
          <template #default="{data}">
            <el-row type="flex" justify="space-between" align="middle" style="height: 40px; width:100%;">
              <el-col :span="20">
                <span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <!-- 下拉菜单 element -->
                  <el-col>
                    <el-dropdown>
                      <span>
                        <svg-icon icon-class="edit" style="margin-right:5px;" />
                        操作<i class="el-icon-arrow-down" />
                      </span>
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="addSonDepartment(data.id)">添加子部门</el-dropdown-item>
                        <el-dropdown-item @click.native="editSonDepartment(data.id)">编辑部门</el-dropdown-item>
                        <el-dropdown-item
                          v-if="!data.children.length"
                          @click.native="delSonDepartment(data.id)"
                        >删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>
      <el-dialog
        title="新增或编辑"
        :visible.sync="showDialog"
        :close-on-click-modal="false"
        :close-on-press-escape="false"
        @close="hClose"
      >
        <DeptDialog
          ref="updateForm"
          :pid="pid"
          :is-edit="isEdit"
          :origin-list="originList"
          @success="hSuccess"
          @close="showDialog=false"
        />
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { getDepartment, delDepartmentById } from '@/api/department'
import { transTreeData } from '@/utils/transTreeData'
import DeptDialog from './deptDialog.vue'
export default {
  components: { DeptDialog },
  data() {
    return {
      list: [],
      showDialog: false,
      pid: '',
      isEdit: false,
      originList: []
    }
  },
  created() {
    this.localDepartment()
  },
  methods: {
    hClose() {
      this.$refs.updateForm.resetForm()
    },
    //! 获取所有部门数据
    async localDepartment() {
      try {
        const res = await getDepartment()
        //* 筛选数据
        this.originList = res.data.depts.map(item => {
          return { id: item.id, pid: item.pid, name: item.name, code: item.code }
        })
        console.log('所有部门信息', res)
        res.data.depts.shift()
        this.list = transTreeData(res.data.depts)
      } catch (error) {
        console.log(error)
      }
    },
    // 获取部门id
    addSonDepartment(id) {
      console.log('部门id', id)
      this.pid = id
      this.showDialog = true
      this.isEdit = false
    },
    // 编辑子部门
    editSonDepartment(id) {
      this.pid = id
      this.showDialog = true
      this.isEdit = true
      // 更新form表单
      // this.$nextTick(() => {
      //   this.$refs.updateForm.loadDepartmentByIdDetail()
      // })
    },
    // 删除子部门
    async delSonDepartment(id) {
      await this.$confirm('你确定删除吗?')
      try {
        const res = await delDepartmentById(id)
        console.log('删除子部门', res)
        this.localDepartment()
      } catch (error) {
        console.log(error)
      }
    },
    // 子组件通知成功的回调
    hSuccess() {
      this.localDepartment()
      this.showDialog = false
    }
  }
}
</script>
